﻿@page "/admin/listbackuparchives"
@attribute [Authorize(Roles = "Admin")]

@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<MDCard>
    <DataTableCard TModel="BackUpArchiveOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl)" ShowAddButton="false" Title="@($"{_name}列表")" @ref="dataTableCard">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <MIcon Color="info"
                       Class="mr-2"
                       OnClick="()=>ExecuteItem(context.Item)">@IconType.Execute.ToIconString()</MIcon>
            }
            else if (context.Header.Value == nameof(BackUpArchiveOverviewModel.Type))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.BackUpArchives.TypeChip Type="context.Item.Type" Name="@context.Item.ObjectName" Id="@context.Item.ObjectId" />
            }
            else if (context.Header.Value == nameof(BackUpArchiveOverviewModel.IsLastFail))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.BackUpArchives.ResultChip IsLastFail="context.Item.IsLastFail" />
            }
            else if (context.Header.Value == nameof(BackUpArchiveOverviewModel.LastBackUpTime))
            {
                @context.Item.LastBackUpTime.ToString("yyyy-MM-dd HH:mm")
            }
            else if (context.Header.Value == nameof(BackUpArchiveOverviewModel.LastTimeUsed))
            {
                @context.Item.LastTimeUsed.ToString("0.0 秒")
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>


@code {
    private bool _editDialog;
    private long _editedItemId;
    private bool _detailDialog;

    private string _baseUrl = "api/backuparchives/list";
    private string _name = "备份";

    private List<DataTableHeader<BackUpArchiveOverviewModel>> _headers = new List<DataTableHeader<BackUpArchiveOverviewModel>>
    {
        new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(EntryInformationTypeOverviewModel.Id)
          },
          new (){ Text= "执行时间", Value= nameof(BackUpArchiveOverviewModel.LastBackUpTime)},
          new (){ Text= "用时", Value= nameof(BackUpArchiveOverviewModel.LastTimeUsed)},
          new (){ Text= "目标", Value= nameof(BackUpArchiveOverviewModel.Type),Sortable=false},        
          new (){ Text= "状态", Value= nameof(BackUpArchiveOverviewModel.IsLastFail)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<BackUpArchiveOverviewModel> dataTableCard;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    public async Task ExecuteItem(BackUpArchiveOverviewModel model)
    {
        const string operationName = "执行备份";
        try
        {
            var obj = await _httpService.PostAsync<RunBackUpArchiveModel, Result>("api/BackUpArchives/RunBackUpArchive", new RunBackUpArchiveModel { Ids = new long[]{model.Id} });
            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败",  AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }
}
